ARG SERVER_TYPE=fastapi_server

# === 使用 poetry 构建 Python 环境 ===
FROM python:3.11-buster as builder

RUN pip install poetry==1.8.2 -i https://pypi.tuna.tsinghua.edu.cn/simple/

ENV POETRY_NO_INTERACTION=1 \
    POETRY_VIRTUALENVS_IN_PROJECT=1 \
    POETRY_VIRTUALENVS_CREATE=1 \
    POETRY_CACHE_DIR=/tmp/poetry_cache

WORKDIR /app

# 复制依赖文件
COPY pyproject.toml poetry.lock ./

# 安装依赖
RUN poetry install --no-root && rm -rf $POETRY_CACHE_DIR

# === 基础运行时服务器镜像 ===
FROM python:3.11-slim AS base_server

#SHELL ["/bin/bash", "-c"]
#
#RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources \
#    && apt-get update \
#    && apt-get install -y --no-install-recommends supervisor \
#    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# 复制项目文件
COPY src ./src
COPY config ./config
COPY static ./static
COPY templates ./templates
COPY resources ./resources
COPY funboost_cli_user.py ./funboost_cli_user.py
COPY funboost_config.py ./funboost_config.py
COPY nb_log_config.py ./nb_log_config.py
COPY main.py .

# 从构建阶段复制虚拟环境
COPY --from=builder /app/.venv /app/.venv

# 设置 Python 路径
ENV PATH="/app/.venv/bin:$PATH"

# === FastAPI 服务器镜像 ===
FROM base_server AS fastapi_server

EXPOSE 9000

CMD ["python", "-m", "main"]

# === Funboost 服务器镜像 ===
FROM base_server AS funboost

CMD ["python", "-m", "funboost_cli_user"]

# 构建最终镜像
FROM ${SERVER_TYPE}
